#import "Common.h"
#import "LevelAchievement.h"

struct LevelAchievementData
{
	NSString*	name;			// 名称
	NSString*	fileName;		// 文件名
	NSString*	description;	// 说明
	int			gold[3];		// 获得金币
	int			require[3];		// 要求
};

LevelAchievementData sLevelAchievementData[LEVELACHIEVEMENT_COUNT] = 
{
 { @"LATotal",		@"achieve-base.png",	@"LATotalDesc",		{5, 10, 15},  { 350, 400, 450 } },			// LAT_Total
 { @"LAMultiplier",	@"achieve-bonus.png",	@"LAMultiplierDesc",{10, 20, 40}, { 50, 80, 110 } },			//LAT_Bonus,
 { @"LASpell",		@"achieve-spell.png",   @"LASpellDesc",		{10, 20, 40}, { 1, 2, 3 } },				// LAT_Spell
 { @"LAScore",		@"achieve-score.png",   @"LAScoreDesc",		{10, 20, 40}, { 300000, 600000, 1000000 } },// LAT_Score
 { @"LAPerfect",	@"achieve-perfect.png", @"LAPerfectDesc",	{10, 20, 30}, {2, 1, 0} },					// LAT_Perfect
 { @"LAPermanent",	@"achieve-last.png",	@"LAPermanentDesc", {10, 20, 40}, {100, 150, 200} }				// LAT_Last
};

@implementation LevelAchieve

@synthesize laType=mType, score=mScore, level=mLevel;

-(id)initWithType:(LevelAchievementType)type
{
	if((self = [super init])) {
		mType = type;
		mScore = 0;
		mLevel = 0;

		int tmpCount = 0;
		
		switch(mType)
		{
			case LAT_Count: 
				for(int i=0; i<INVADERS_COUNT; i++) 
					tmpCount += gCurScore.invadersCount[i];
				break;
			case LAT_Bonus:
				tmpCount = gCurScore.invadersCount[IT_Good];
				break;
			case LAT_Spell:
				tmpCount = gCurScore.spellCastCount;
				break;
			case LAT_Score:
				tmpCount = gCurScore.score;
				break;
			case LAT_Perfect:
				if(gCurScore.invadersCount[IT_Bad] == 2)
					mLevel = 1;
				else if(gCurScore.invadersCount[IT_Bad] == 1)
					mLevel = 2;
				else if(gCurScore.invadersCount[IT_Bad] == 0)
					mLevel = 3;
				break;
			case LAT_Last:
				tmpCount = gCurScore.lastInSurvival;
		}
		
		if(mType != LAT_Perfect)
			for(int i=2; i>=0; i--)
				if(tmpCount >= sLevelAchievementData[mType].require[i]) {
					mLevel = i+1;
					break;
				}
		
		if(mLevel > 0)
			mScore = sLevelAchievementData[mType].gold[mLevel-1];

		CCLabelTTF* labelPoint = [CCLabelTTF labelWithString:[NSString stringWithFormat:@":%d", mScore]
													fontName:gFontName
													fontSize:14];
		labelPoint.position = ccp(-6,-25);
		labelPoint.anchorPoint = gAnchorLeft;
		labelPoint.color = ccYELLOW;
		[self addChild:labelPoint];
		
		CCSprite* goldIcon = [CCSprite spriteWithFile:@"GoldCoin.png"];
		goldIcon.position = ccp(-7,-25);
		goldIcon.anchorPoint = gAnchorRight;
		goldIcon.scale = 0.7;
		[self addChild:goldIcon];
		
		mIcon = [CCSprite spriteWithFile:sLevelAchievementData[mType].fileName];
		if(mLevel == 0)
			mIcon.color = ccc3(100,100,100);
		[self addChild:mIcon ];
		[self autorelease];
	}
	return self;
}

-(bool)checkClick:(UITouch*)touch
{
	CGPoint pos = [self convertTouchToNodeSpace:touch];
	CGSize size = mIcon.contentSize;
	CGRect rc = CGRectMake(-size.width/2, -size.height/2, size.width, size.height);
	return CGRectContainsPoint(rc, pos);
}

@end

@implementation LevelAchievements

-(id)init
{
	if ((self = [super init])) {
		mAchieves = [[NSMutableArray alloc] init];
		// 两种模式都有的成就
		LevelAchieve* la;
		for(int i = 0; i < LEVELACHIEVEMENT_COUNT-1; i++) {
			if(i == LEVELACHIEVEMENT_COUNT-2) {
				if(gCurScore.gameMode == GM_Time) {
					la = [[LevelAchieve alloc] initWithType:LAT_Perfect];
				}	else {
					la = [[LevelAchieve alloc] initWithType:LAT_Last];
				}
			} else {
				la = [[LevelAchieve alloc] initWithType:(LevelAchievementType)i];
			}
		
			la.anchorPoint = gAnchorLeft;
			la.position = ccp(i*70+30,-20);
			[self addChild:la];
			[mAchieves addObject:la];
		}
		
		mBgDesc = [CCSprite spriteWithFile:@"achieve-desc-bg.png"];
		mBgDesc.visible = false;
		[self addChild:mBgDesc];
		
		mLabelName = [CCLabelTTF labelWithString:@"" fontName:gFontName fontSize:14];
		mLabelName.color = ccMAGENTA;
		mLabelName.position = ccp(mBgDesc.contentSize.width/2,60);
		[mBgDesc addChild:mLabelName];
		
		mLabelDesc = [CCLabelTTF labelWithString:@""
									  dimensions:CGSizeMake(130,50)
									   alignment:CCTextAlignmentCenter
										fontName:gFontName fontSize:12];
		mLabelDesc.color = ccBLACK;
		mLabelDesc.position = ccp(mBgDesc.contentSize.width/2,25);
		[mBgDesc addChild:mLabelDesc];
		
		mLabelNext = [CCLabelTTF labelWithString:@"" fontName:gFontName fontSize:12];
		mLabelNext.color = ccMAGENTA;
		mLabelNext.position = ccp(mBgDesc.contentSize.width/2,10);
		[mBgDesc addChild:mLabelNext];
	}
	return self;
}

-(void)dealloc
{
	[mAchieves release];
	[super dealloc];
}

-(void)showDescription:(UITouch*)touch
{
	bool haveShow = false;
	LevelAchieve* la;
	for(la in mAchieves) {
		if([la checkClick:touch]) {
			LevelAchievementData& lad = sLevelAchievementData[la.laType];
			mLabelName.string = [NSString stringWithFormat:@"%@ Lv.%d", NSLocalizedString(lad.name,nil), la.level];

			if(la.laType == LAT_Score) {
				if(la.level == 3)
					mLabelNext.string = [NSString stringWithFormat:@"%@",
										 NSLocalizedString(@"LANextNone",nil)];
				else
					mLabelNext.string = [NSString stringWithFormat:@"%@ %s",
										 NSLocalizedString(@"LANext",nil),
										 commaprint(lad.require[la.level])];
				mLabelDesc.string = [NSString stringWithFormat:NSLocalizedString(lad.description,nil),
									 commaprint(lad.require[MAX(0,la.level-1)])];
			}
			else {
				if(la.level == 3)
					mLabelNext.string = [NSString stringWithFormat:@"%@",
										 NSLocalizedString(@"LANextNone",nil)];
				else
					mLabelNext.string = [NSString stringWithFormat:@"%@ %d",
										 NSLocalizedString(@"LANext",nil),
										 lad.require[la.level]];
				mLabelDesc.string = [NSString stringWithFormat:NSLocalizedString(lad.description,nil),
									 lad.require[MAX(0,la.level-1)]];
			}			
			
			
			mBgDesc.position = ccp(la.position.x,la.position.y+55);
			mBgDesc.visible = true;
			haveShow = true;
			break;
		}
	}

	if(!haveShow)
		mBgDesc.visible = false;
}

-(int) getAllScore
{
	int score = 0;
	LevelAchieve* la;
	for(la in mAchieves)
		score += la.score;
	return score;
}

@end
